package com.ruoyi.employer.service.impl;

import java.util.List;

import com.ruoyi.employer.domain.PayAlipay;
import com.ruoyi.employer.domain.PayWechat;
import com.ruoyi.employer.mapper.CargoUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.employer.mapper.PayBalanceMapper;
import com.ruoyi.employer.domain.PayBalance;
import com.ruoyi.employer.service.IPayBalanceService;

/**
 * 货主余额管理Service业务层处理
 * 
 * @author ysq
 * @date 2024-08-14
 */
@Service
public class PayBalanceServiceImpl implements IPayBalanceService 
{
    @Autowired
    private PayBalanceMapper payBalanceMapper;
    @Autowired
    private CargoUserMapper cargoUserMapper;

    /**
     * 查询货主余额管理
     * 
     * @param id 货主余额管理主键
     * @return 货主余额管理
     */
    @Override
    public PayBalance selectPayBalanceById(Long id)
    {
        return payBalanceMapper.selectPayBalanceById(id);
    }

    /**
     * 查询货主余额管理列表
     * 
     * @param payBalance 货主余额管理
     * @return 货主余额管理
     */
    @Override
    public List<PayBalance> selectPayBalanceList(PayBalance payBalance)
    {
        return payBalanceMapper.selectPayBalanceList(payBalance);
    }

    /**
     * 新增货主余额管理
     * 
     * @param payBalance 货主余额管理
     * @return 结果
     */
    @Override
    public int insertPayBalance(PayBalance payBalance)
    {
        return payBalanceMapper.insertPayBalance(payBalance);
    }

    /**
     * 修改货主余额管理
     * 
     * @param payBalance 货主余额管理
     * @return 结果
     */
    @Override
    public int updatePayBalance(PayBalance payBalance)
    {
        return payBalanceMapper.updatePayBalance(payBalance);
    }

    /**
     * 批量删除货主余额管理
     * 
     * @param ids 需要删除的货主余额管理主键
     * @return 结果
     */
    @Override
    public int deletePayBalanceByIds(Long[] ids)
    {
        return payBalanceMapper.deletePayBalanceByIds(ids);
    }

    /**
     * 删除货主余额管理信息
     * 
     * @param id 货主余额管理主键
     * @return 结果
     */
    @Override
    public int deletePayBalanceById(Long id)
    {
        return payBalanceMapper.deletePayBalanceById(id);
    }
    /**
     * 充值操作
     *
     * @param id 用户ID
     * @param amount 充值金额
     * @return 更新是否成功
     */
    @Override
    public boolean recharge(Long id, double amount) {
        PayBalance payBalance = payBalanceMapper.selectById(id);
        if (payBalance == null) {
            return false; // 用户不存在
        }
        // 计算新的金额
        Double newBalanceWallet = payBalance.getBalanceWallet() + amount;
        payBalance.setBalanceWallet(newBalanceWallet);
        int rows = payBalanceMapper.updatePayBalance(payBalance);
        return rows > 0;
    }


    @Override
    public boolean withdraw(Long id, double amount) {
        // 从数据库获取 PayWechat 对象
        PayBalance payBalance = payBalanceMapper.selectById(id);
        if (payBalance == null) {
            return false; // 用户不存在
        }

        // 获取钱包金额并进行计算
        Double balanceWallet = payBalance.getBalanceWallet();
        if (balanceWallet == null) {
            balanceWallet = 0.0; // 如果钱包金额为 null，则将其视为 0
        }

        // 计算新的金额
        Double newBalanceWallet = balanceWallet - amount;
        if (newBalanceWallet < 0) {
            return false; // 余额不足
        }

        payBalance.setBalanceWallet(newBalanceWallet);
        int rows = payBalanceMapper.updatePayBalance(payBalance);
        return rows > 0;
    }

    @Override
    public boolean transfer(Long fromId, String toPhoneNumber, double amount) {
        // 查找转出账户
        PayBalance fromAccount = payBalanceMapper.selectById(fromId);
        if (fromAccount == null) {
            return false; // 转出账户不存在
        }

        // 根据手机号查找转入账户的ID
        Long toId = cargoUserMapper.findUserIdByPhoneNumber(toPhoneNumber);
        if (toId == null) {
            return false; // 转入账户不存在
        }

        // 查找转入账户
        PayBalance toAccount = payBalanceMapper.selectById(toId);
        if (toAccount == null) {
            return false; // 转入账户不存在
        }

        // 检查余额
        if (fromAccount.getBalanceWallet() < amount) {
            return false; // 余额不足
        }

        // 执行转账
        fromAccount.setBalanceWallet(fromAccount.getBalanceWallet() - amount);
        toAccount.setBalanceWallet(toAccount.getBalanceWallet() + amount);

        // 更新数据库
        int updateFrom = payBalanceMapper.updatePayBalance(fromAccount);
        int updateTo = payBalanceMapper.updatePayBalance(toAccount);

        if (updateFrom > 0 && updateTo > 0) {
            return true; // 转账成功
        } else {
            return false; // 转账失败
        }
    }

}
